package com.jacen.fzxs.manager;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jacen.fzxs.controller.LoginController;
import com.jacen.fzxs.entity.UserEntity;

/**********
 * 功能：通过监听session属性的add来保证list相同名字usid的session只有一个有效;
 * 参数sessions用于存放有效的session
 * **********/
public class UserOnlineListerner implements HttpSessionListener,
		HttpSessionAttributeListener {
	
	private static Logger logger = LoggerFactory.getLogger(UserOnlineListerner.class);

	private static List <HttpSession> sessions;
	static {
		if (sessions == null) {
			sessions = Collections.synchronizedList(new ArrayList<HttpSession>());
		}
	}

	public void sessionCreated(HttpSessionEvent e) {
	}

	public void sessionDestroyed(HttpSessionEvent event) {
		HttpSession httpSession=event.getSession();
		UserEntity nowTuser=(UserEntity)httpSession.getAttribute(LoginController.USERNAME);
		if(nowTuser==null)
			return;
		
		for (int i = sessions.size()-1; i >= 0; i--) {
			UserEntity user=null;
			try {
				user=(UserEntity)sessions.get(i).getAttribute(LoginController.USERNAME);
				if(user==null){
					sessions.remove(i);
				}else if(user.getUserName().equals(nowTuser.getUserName())){
					//sessions.get(i).invalidate();
					sessions.remove(i);
					sync_refresh();
					break;
				}
			} catch (Exception e) {
				logger.error(e.getMessage());
				sessions.remove(i);
				continue;
			}
		}
	}

	//同步刷新页面
	private void sync_refresh() {
		
		
	}

	public void attributeAdded(HttpSessionBindingEvent e) {
		String addName=e.getName();
		if(LoginController.USERNAME.equals(addName)){
			synchronized(e){
				HttpSession nowSession = e.getSession();
				UserEntity nowTuser=(UserEntity)e.getValue();
				for (int i = sessions.size()-1; i >= 0; i--) {
					UserEntity user=null;
					try {
						user=(UserEntity)sessions.get(i).getAttribute(LoginController.USERNAME);
						if(user==null){
							sessions.remove(i);
						}else if(user.getUserName().equals(nowTuser.getUserName())){
							sessions.get(i).invalidate();//invalidate会触发attributeRemoved事件，里面会有remove动作
							//sessions.remove(i);
							break;
						}
					} catch (Exception e2) {
						logger.error(e2.getMessage());
						sessions.remove(i);
						continue;
					}
				}
				
				sessions.add(nowSession);
			}
			
		}
	}

	public void attributeRemoved(HttpSessionBindingEvent e) {
		String addName=e.getName();
		if(LoginController.USERNAME.equals(addName)){
			synchronized(e){
				UserEntity nowTuser=(UserEntity)e.getValue();
				for (int i = sessions.size()-1; i >= 0; i--) {
					UserEntity user=null;
					try {
						user=(UserEntity)sessions.get(i).getAttribute(LoginController.USERNAME);
						if(user==null){
							sessions.remove(i);
						}else if(user.getUserName().equals(nowTuser.getUserName())){
							//sessions.get(i).invalidate();
							sessions.remove(i);
							break;
						}	
					} catch (Exception e2) {
						logger.error(e2.getMessage());
						sessions.remove(i);
						continue;
					}
				}
			}
		}

	}

	public void attributeReplaced(HttpSessionBindingEvent e) {

	}

}
